<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<html>
<head>
<title>代码表管理</title>
<script type="text/javascript">
	var $grid;
	var $codeValuesTreeGrid;
	var selectTableRow;//选中的行数据
	$(function() {
		$grid = $("#grid").datagrid(
				$.extend(datagridOption, {
					url : "${ctx}/basic/codeTable/dataGrid",
					columns : [ [
							{
								field : 'ck',
								checkbox : true
							},
							{
								field : 'tableName',
								title : '表名称',
								width : 120,
								sortable : true
							},
							{
								field : 'tableCode',
								title : '标识码',
								width : 120
							},
							{
								field : 'remark',
								title : '备注',
								width : 200,
								sortable : true
							},
							{
								field : 'enable',
								title : '是否启用',
								width : 100,
								formatter : function(value, row, index) {
									return '<input class="easyui-switchbutton "' + (value == 1 ? "checked" : "")
											+ ' data-options="readonly:true,onText:\'是\',offText:\'否\'">';
								}
							} ] ],
					onClickRow : function(rowIndex, rowData) {
						selectTableRow = rowData;
						$("#cc").layout("panel", "east").panel({
							title : "代码表【" + rowData.tableName + "】明细管理"
						});

						$codeValuesTreeGrid.treegrid("clearSelections");//清除所有选中的行，解决切换不同代码表后还保留代码值中上次选中行bug
						refreshCodeValues(rowData);//刷新代码表明细
					}
				}));
		$grid.datagrid('enableFilter', [ {
			field : 'enable',
			type : 'combobox',
			javaType : 'I',
			options : {
				panelHeight : 'auto',
				data : [ {
					value : '',
					text : '全部'
				}, {
					value : '1',
					text : '是'
				}, {
					value : '2',
					text : '否'
				} ],
				onChange : function(value) {
					if (value == '') {
						$grid.datagrid('removeFilterRule', 'enable');
					} else {
						$grid.datagrid('addFilterRule', {
							field : 'enable',
							op : 'EQ',
							value : value
						});
					}
					$grid.datagrid('doFilter');
				}
			}
		} ]);
		$("#enableSB").switchbutton({
			onText : "是",
			offText : "否",
			checked : true,
			onChange : function(checked) {
				if (checked) {
					$("#enable").val(1);
				} else {
					$("#enable").val(2);
				}
			}
		});

		$codeValuesTreeGrid = $("#codeValuesTreeGrid").treegrid(
				$.extend(datagridOption, {
					method : "get",
					url : "",
					idField : 'id',
					treeField : 'codeName',
					parentField : "pid",
					remoteSort : true,
					multiSort : true,
					rownumbers : true,
					singleSelect : true,
					striped : true,
					selectOnCheck : false,
					checkOnSelect : false,
					fit : true,
					cascadeCheck : true,//级联勾选
					dataPlain : true, //该属性用以启用当前 easyui-treegrid 控件对平滑数据格式的支持
					toolbar : '#codett',
					columns : [ [
							{
								field : 'ck',
								checkbox : true
							},
							{
								field : 'codeTable',
								title : '所属代码表',
								sortable : true,
								formatter : function(value, row, index) {
									return value ? value.tableName : "";
								}
							},
							{
								field : 'codeName',
								title : '代码名称',
								sortable : true,
								editor : 'textbox'
							},
							{
								field : 'parentCodeName',
								title : '父代码名称',
								editor : {
									type : 'combobox',
									options : {
										valueField : 'productid',
										textField : 'productname',
										method : 'get',
										required : true
									}
								}
							},
							{
								field : 'codeValue',
								title : '代码值',
								sortable : true,
								editor : 'textbox'
							},
							{
								field : 'remark',
								title : '备注',
								sortable : true,
								editor : 'textbox'
							},
							{
								field : 'enable',
								title : '是否启用',
								formatter : function(value, row, index) {
									return '<input class="easyui-switchbutton "' + (value == 1 ? "checked" : "")
											+ ' data-options="readonly:true,onText:\'是\',offText:\'否\'">';
								},
								editor : {
									type : 'checkbox',
									options : {
										on : '是',
										off : '否'
									}
								}
							}, {
								field : 'iconCls',
								title : '图标Class',
								hidden : true,
								editor : 'textbox'
							} ] ],
					onClickRow : function(rowIndex, rowData) {

					},
					onLoadSuccess : function() {
						$("#codeValuesTreeGrid").parent().find(".datagrid-btable .easyui-switchbutton").switchbutton();
					},//异步加载完毕时手动绘制switchbutton
					enableHeaderClickMenu : true, //此属性开启表头列名称右侧那个箭头形状的鼠标左键点击菜单
					enableHeaderContextMenu : true, //此属性开启表头列名称右键点击菜单
					enableRowContextMenu : false
				}));

		$("#codeEnableSB").switchbutton({
			onText : "是",
			offText : "否",
			checked : true,
			onChange : function(checked) {
				if (checked) {
					$("#codeEnable").val(1);
				} else {
					$("#codeEnable").val(2);
				}
			}
		});

		$("#add").click(function() {
			$("#fm").form("clear");
			$('#fm').form('load', {
				enable : 1
			});
			$("#enableSB").switchbutton('check');
			//$("#radio1").click();

			$("#dlg").dialog("open").dialog("setTitle", "新增代码表");
		});
		$("#codeadd").click(function() {
			fn_initCombo();
			if (!selectTableRow) {
				$.messager.show({
					icon : "error",
					msg : "请先选中代码表中的行"
				});
				return false;
			}
			$("#codefm").form("clear");
			var codeSelected = $codeValuesTreeGrid.datagrid("getSelected");
			$('#codefm').form('load', {
				enable : 1,
				codeTable : selectTableRow.id,
				codeParentId : !codeSelected ? '' : codeSelected.id
			});
			$("#codeEnableSB").switchbutton('check');
			//$("#radio3").click();

			$("#codedlg").dialog("open").dialog("setTitle", "新增代码");
		});
		$("#edit").click(function() {
			var rowData = $grid.datagrid("getSelected");
			if (rowData == null) {
				$.messager.show({
					msg : "您未选中行，无法编辑",
					icon : "warning",
					position : "bottomRight"
				});
			} else {
				if (rowData.editable != 1) {
					$.messager.show({
						msg : "该代码表不可编辑",
						icon : "warning",
						position : "bottomRight"
					});
				} else {
					$("#dlg").dialog("open").dialog("setTitle", "编辑代码表");
					$("#fm").form("load", rowData);
					$("#enableSB").switchbutton(rowData.enable == 1 ? "check" : "uncheck");
				}
			}
		});
		$("#codeedit").click(function() {
			if (!selectTableRow) {
				showWarnInfo("请先选中代码表中的行");
				return false;
			}
			if (selectTableRow.editable != 1) {
				showWarnInfo("该代码表不可编辑");
				return false;
			}
			fn_initCombo();
			var rowData = $codeValuesTreeGrid.datagrid("getSelected");
			if (rowData == null) {
				$.messager.show({
					msg : "您未选中行，无法编辑",
					icon : "warning",
					position : "bottomRight"
				});
			} else {
				$("#codefm").form("load", $.extend(rowData, {
					codeTable : selectTableRow.id,
				}));
				$("#codeEnableSB").switchbutton(rowData.enable == 1 ? "check" : "uncheck");
				$("#codedlg").dialog("open").dialog("setTitle", "编辑代码");
				$("#iconCls").comboicons("setValue", rowData.iconCls);
			}
		});
		$("#delete").click(function() {
			/* multiDeleteDataGrid($grid,"${ctx}/basic/codeTable/delete");
			$codeValuesTreeGrid.treegrid("load"); */
			var rowData = $grid.datagrid("getChecked");
			if (rowData == null) {
				$.messager.show({
					msg : "您未选中行，无法删除",
					icon : "warning",
					position : "bottomRight"
				});
			} else {
				var ids = "";
				$.messager.confirm("您确定要进行该操作？", function(c) {
					if (c) {
						for (var i = 0; i < rowData.length; i++) {
							ids += rowData[i].id + ",";
						}
						ids = ids.substring(0, ids.length - 1);
						$.post("${ctx}/basic/codeTable/delete", {
							ids : ids
						}, function(result) {
							$.messager.show({
								msg : result.msg,
								icon : "info",
								position : "bottomRight"
							});
							if (result.success) {
								$grid.datagrid("uncheckAll");//zst添加，清除上次删除选中的行，解决连续删除时id不存在的bug
								$grid.datagrid("load");
								$codeValuesTreeGrid.treegrid("load");
							}
						}, 'json');
					}
				});
			}
		});
		$("#codedelete").click(function() {
			multiDeleteTreeGrid($codeValuesTreeGrid, "${ctx}/basic/codeValues/delete");
		});

		$("#codeall").click(function() {
			$codeValuesTreeGrid.treegrid("clearSelections");//清除所有选中的行，解决始终保留代码值中上次选中行bug
			$codeValuesTreeGrid.treegrid({
				url : "${ctx}/basic/codeValues/dataGrid"
			});
		});

		$("#excelImport").click(function() {
			$.easyui.importor.showImportor({
				importNo : "CodeTable",
				onExportSuccess : function(file, data) {
					$grid.datagrid('load');
				}
			});
		});
		$("#excelImportValues").click(function() {
			$.easyui.importor.showImportor({
				importNo : "CodeValues",
				onExportSuccess : function(file, data) {
					$codeValuesTreeGrid.treegrid({
						url : "${ctx}/basic/codeValues/dataGrid?search_EQ_codeTable.id=" + selectTableRow.id
					});
				}
			});
		});

		$("#excelExport").click(function() {
			var url = "${ctx}/basic/codeTable/excelExport?" + $.param($grid.datagrid('options').queryParams);
			if ($grid.datagrid("options").filterRules) {
				url += "&filterRules=" + JSON.stringify($grid.datagrid("options").filterRules);
			}
			window.parent.location.href = url;
		});
		$("#excelExportValues").click(
				function() {
					if (selectTableRow != null) {
						var url = "${ctx}/basic/codeTable/excelExportValues?search_EQ_codeTable.id=" + selectTableRow.id
								+ "&search_LIKE_codeName=" + $("#search_LIKE_codeName").val();
					} else {
						var search_LIKE_codeName = $("#search_LIKE_codeName").val();
						if (search_LIKE_codeName != null) {
							var url = "${ctx}/basic/codeTable/excelExportValues?search_LIKE_codeName=" + $("#search_LIKE_codeName").val();
						} else {
							var url = "${ctx}/basic/codeTable/excelExportValues?" + $.param($grid.datagrid('options').queryParams);
						}
					}
					if ($grid.datagrid("options").filterRules) {
						url += "&filterRules=" + JSON.stringify($grid.datagrid("options").filterRules);
					}
					window.parent.location.href = url;
				});
	});
	function fn_initCombo() {
		$('#codeParentId').combotree({
			url : '${ctx}/basic/codeValues/comboTree?search_EQ_codeTable.id=' + selectTableRow.id,
			method : 'get',
			editable : false,
			idField : 'id',
			textField : 'text',
			parentField : 'pid',
			dataPlain : true
		});
	}
	function fn_save() {
		$("#fm").form("submit", {
			url : "${ctx}/basic/codeTable/update",
			success : function(data) {
				$grid.datagrid("load");
				$.messager.show({
					msg : "保存成功",
					icon : "info",
					position : "bottomRight"
				});
				$("#dlg").dialog('close');
			}
		});
	}
	function fn_codesave() {
		$("#codefm").form("submit", {
			url : "${ctx}/basic/codeValues/update",
			success : function(data) {
				$codeValuesTreeGrid.treegrid("clearSelections");//清除所有选中的行，解决始终保留代码值中上次选中行bug
				$codeValuesTreeGrid.treegrid({
					url : "${ctx}/basic/codeValues/dataGrid?search_EQ_codeTable.id=" + selectTableRow.id
				});
				showInfoForCallBack(data);
				$("#codedlg").dialog('close');
			}
		});
	}

	//刷新values列表
	function refreshCodeValues(rowData) {
		if (!rowData) {
			showAlert("请先选择中类信息");
			return false;
		} else {
			$codeValuesTreeGrid.treegrid({
				url : "${ctx}/basic/codeValues/dataGrid?search_EQ_codeTable.id=" + rowData.id
			});
			$codeValuesTreeGrid.treegrid('reload', {
				search_LIKE_codeName : ''
			});
		}
	}
	var editIndex = undefined;
	function endEditing() {
		if (editIndex == undefined) {
			return true
		}
		if ($('#codeValuesTreeGrid').treegrid('validateRow', editIndex)) {
			var ed = $('#codeValuesTreeGrid').treegrid('getEditor', {
				index : editIndex,
				field : 'productid'
			});
			var productname = $(ed.target).combobox('getText');
			$('#dg').treegrid('getRows')[editIndex]['productname'] = productname;
			$('#dg').treegrid('endEdit', editIndex);
			editIndex = undefined;
			return true;
		} else {
			return false;
		}
	}
	function onClickCell(index, field) {
		if (editIndex != index) {
			if (endEditing()) {
				$('#dg').datagrid('selectRow', index).datagrid('beginEdit', index);
				var ed = $('#dg').datagrid('getEditor', {
					index : index,
					field : field
				});
				($(ed.target).data('textbox') ? $(ed.target).textbox('textbox') : $(ed.target)).focus();
				editIndex = index;
			} else {
				$('#dg').datagrid('selectRow', editIndex);
			}
		}
	}
	function append() {
		if (endEditing()) {
			$('#codeValuesTreeGrid').treegrid('append', {
				data : [ {
					id : 0,
					enable : 1
				} ]
			}).treegrid('beginEdit', 0);
		}
	}
	function removeit() {
		if (editIndex == undefined) {
			return;
		}
		$('#dg').datagrid('cancelEdit', editIndex).datagrid('deleteRow', editIndex);
		editIndex = undefined;
	}
	function accept() {
		if (endEditing()) {
			$('#dg').datagrid('acceptChanges');
		}
	}
	function reject() {
		$('#dg').datagrid('rejectChanges');
		editIndex = undefined;
	}
	function getChanges() {
		var rows = $('#dg').datagrid('getChanges');
		alert(rows.length + ' rows are changed!');
	}
</script>
</head>

<body>
	<div data-options="region:'center'" data-options="fit:true,border:false" style="padding: 1px;">
		<table id="grid">
		</table>
		<div id="tt">
			<shiro:hasPermission name="codeTable:update">
				<a id="add" class="easyui-linkbutton easyui-tooltip" data-options="plain: true, iconCls: 'icon-standard-add',content:'新增'">新增</a>
				<a id="edit" class="easyui-linkbutton easyui-tooltip" data-options="plain: true, iconCls: 'icon-hamburg-pencil',content:'编辑'">编辑</a>
			</shiro:hasPermission>
			<shiro:hasPermission name="codeTable:delete">
				<a id="delete" class="easyui-linkbutton easyui-tooltip" data-options="plain: true, iconCls: 'icon-hamburg-busy',content:'删除'">删除</a>
			</shiro:hasPermission>
			<shiro:hasPermission name="codeTable:import">
				<span class="dialog-tool-separator"></span>
				<a id="excelImport" class="easyui-linkbutton easyui-tooltip"
					data-options="plain: true, iconCls: 'icon-standard-page-white-excel',content:'Excel导入'">Excel导入</a>
			</shiro:hasPermission>
			<shiro:hasPermission name="codeTable:export">
				<a id="excelExport" class="easyui-linkbutton" data-options="plain: true, iconCls: 'icon-standard-page-excel'">Excel导出</a>
			</shiro:hasPermission>
		</div>
		<shiro:hasPermission name="codeTable:update">
			<div id="dlg" class="easyui-dialog" style="width: 90%; height: 90%; padding: 10px"
				data-options="iconCls:'icon-standard-shape-move-forwards',resizable:true,modal:true, 
						toolbar: [{
		                    text:'保存',
		                    iconCls:'icon-save',
		                    handler:function(){
		                        alert('save')
		                    }
		                }],
					buttons:[
		                    { text: '保存', iconCls: 'icon-ok', handler: function () { fn_save(); } },
		                    { text: '取消', iconCls:'icon-cancel', handler: function () { $('#dlg').dialog('close'); } }
		                ],closed:true">
				<div class="easyui-layout" data-options="fit:true">
					<div data-options="region:'north',split:true" style="height: 80px;">
						<form id="fm" method="post">
							<input type="hidden" name="id" />
							<table id="tbl">
								<tr>
									<td><label>代码表名称:</label></td>
									<td><input id="tableName" name="tableName" class="easyui-validatebox" data-options="required:true" /></td>
									<td><label>标识码:</label></td>
									<td><input name="tableCode" class="easyui-validatebox" data-options="required:true" /></td>
									<td><label>是否启用:</label></td>
									<td><input type="hidden" id="enable" name="enable" /> <input id="enableSB" /></td>
									<td><label>备注:</label></td>
									<td><input name="remark" class="easyui-textbox"></td>
								</tr>
							</table>
						</form>
					</div>
					<div data-options="region:'center'">
						<table id="codeValuesTreeGrid">
						</table>
						<div id="codett" class="easyui-toolbar">
							<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="append()">Append</a>
							<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="removeit()">Remove</a>
							<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:true" onclick="accept()">Accept</a>
							<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-undo',plain:true" onclick="reject()">Reject</a>
							<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true" onclick="getChanges()">GetChanges</a>

							<shiro:hasPermission name="codeTable:update">
								<a id="codeadd" class="easyui-linkbutton easyui-tooltip" data-options="plain: true, iconCls: 'icon-standard-add',content:'新增'"></a>
								<a id="codeedit" class="easyui-linkbutton easyui-tooltip" data-options="plain: true, iconCls: 'icon-hamburg-pencil',content:'编辑'"></a>
							</shiro:hasPermission>
							<shiro:hasPermission name="codeTable:delete">
								<a id="codedelete" class="easyui-linkbutton easyui-tooltip" data-options="plain: true, iconCls: 'icon-hamburg-busy',content:'删除'"></a>
							</shiro:hasPermission>
							<a id="codeall" class="easyui-linkbutton easyui-tooltip" data-options="plain: true, iconCls: 'icon-hamburg-zoom',content:'显示全部'"></a>
							<shiro:hasPermission name="codeTable:import">
								<a id="excelImportValues" class="easyui-linkbutton easyui-tooltip"
									data-options="plain: true, iconCls: 'icon-standard-page-white-excel',content:'Excel导入'"></a>
							</shiro:hasPermission>
							<shiro:hasPermission name="codeTable:export">
								<a id="excelExportValues" class="easyui-linkbutton easyui-tooltip"
									data-options="plain: true, iconCls: 'icon-standard-page-excel',content:'Excel导出'"></a>
							</shiro:hasPermission>
						</div>
					</div>
				</div>
			</div>
		</shiro:hasPermission>
	</div>
</body>
</html>
